<extend name='Public/index'/>
<block name='content'>
<table id='Tbl'>
<thead>
    <tr>
        <th><input type="checkbox" onclick="selectAll(this)" value='全选'></th>
        <th>编号</th>
        <th>菜单名称</th>
        <th>链接地址</th>
        <th>打开方式</th>
        <th>上级菜单</th>
        <th>分组</th>
        <th>排序</th>
        <th>锁定</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <volist name='menus' id='vo'>
        <tr id="row_<{$vo.menu_id}>">
            <td><input type="checkbox" class='select' value='<{$vo.menu_id}>' ></td>
            <td><{$vo.menu_id}></td>
            <td><input name="mname" class="clss" disabled type="text" value="<{$vo.mname}>"></td>
            <td><input name="url" class="clss" disabled type="text" value="<{$vo.url}>"></td>
            <td>
                    <div class="switch tiny">
                        <input onclick="target(this)" value="<{$vo.menu_id}>" id="target_<{$vo.menu_id}>" type="checkbox"  <{$vo['target']?'checked':''}>>
                        <label for="target_<{$vo.menu_id}>"></label>
                    </div>
                </td>
            <td>
                    <select class="clss" disabled name="up" id="">
                        <option <{$vo['up']==0?'selected':null}>value="0">顶级</option>
                        <volist name='opts' id='opt'>
                            <option <{$vo['up']==$opt['menu_id']?'selected':null}> value="<{$opt['menu_id']}>"><{$opt.mname}></option>
                        </volist>
                    </select>
                </td>
            <td><input name="mgroup" class="clss" disabled style="width:4em;" type="text" value="<{$vo.mgroup}>"></td>
            <td><input name="msort" class="clss" disabled style="width:4em;" type="text" value="<{$vo.msort}>"></td>
                
            <td>
            
             <div class="switch tiny">
              <input onclick="lock(this)" value="<{$vo.menu_id}>" id="lock_<{$vo.menu_id}>" type="checkbox"  <{$vo['lock']?'checked':''}>>
                <label for="lock_<{$vo.menu_id}>"></label>
                </div>
               
            </td> 
            <td>
            <button type="button" onclick='edit("<{$vo.menu_id}>")' class='edit tiny'>编辑</button>
            <button style='display:none' onclick='save("<{$vo.menu_id}>")' type="button" class="save tiny">保存</button>
            </td>
        </tr>
    </volist>
    </tbody>
</table>
<{$page}>
<input type="button" class="button tiny" onclick="moreDel()" value='删除(批量)'>
<input type="button" class="button tiny" onclick="unSelect()" value='反选'>
<input type="button" class='button tiny' onclick="addMenu()" value='添加'>
<script>
        function selectAll(obj) {
            $('#Tbl tbody .select').prop('checked',$(obj).prop('checked'));
        }
        function unSelect() {
            $('#Tbl tbody .select').map(function(){
                $(this).prop('checked',!$(this).prop('checked'));
            });
        }
        function moreDel() {
            if(!confirm('确定删除吗?')){
                return;
            }
            var ids = $('#Tbl tbody .select:checked').map(function () {
                $(this).parent().parent().remove();
                return $(this).val();
            });
            ids  = ids.get().join();
            $.post('<{:U("doDelMenu")}>',{ids:ids},function (data) {
                if(data.error ==0)alert(data.msg);
            });
        }
        function lock(obj) {
            var id = $(obj).val();
            var lockval = $(obj).prop('checked')?1:0;
            $.post('<{:U("doEditMenu")}>',{menu_id:id,lock:lockval});
        }
        function target(obj) {
            var id = $(obj).val();
            var targetval = $(obj).prop('checked')?1:0;
            $.post('<{:U("doEditMenu")}>',{menu_id:id,target:targetval},function (data) {
            console.log(data);
                        });
        }
        
        function edit(id) {
        	console.log(id);
            $('#row_'+id+' .clss').prop('disabled',false);
            $('#row_'+id+' .edit').hide();
            $('#row_'+id+' .save').show();
        }

        function save(id) {
            $('#row_'+id+' .clss').prop('disabled',true);
            $('#row_'+id+' .edit').show();
            $('#row_'+id+' .save').hide();

            var data = {
                menu_id:id,
                mname:$('#row_'+id+' [name=mname]').val(),
                url:  $('#row_'+id+' [name=url]').val(),
                mgroup :  $('#row_'+id+' [name=mgroup]').val(),
                msort :  $('#row_'+id+' [name=msort]').val(),
                up : $('#row_'+id+' [name=up]').val(),
            }

            $.post('<{:U("doEditMenu")}>',data,function (data) {
                console.log(data);
                if(data.error!==0){
                    alert(data.msg);
                }
            });

        }
    
        function addMenu(){
            $.get("<{:U('lastId')}>", '',
            function(id){
               var id= parseInt(id)+1;

                var html =
            '<tr id="row_'+id+'">'+
'            <td><input type="checkbox" class="select" value="" ></td>'+
'            <td>'+id+'</td>'+
'            <td><input name="mname" class="clss" disabled type="text" value=""></td>'+
'            <td><input name="url" class="clss" disabled type="text" value=""></td>'+
'            <td>'+
'                    <div class="switch tiny">'+
'                        <input onclick="target(this)" value="'+id+'" id="target_'+id+'" type="checkbox"  <{$vo["target"]?"checked":""}>>'+
'                        <label for="target_'+id+'"></label>'+
'                    </div>'+
'                </td>'+
'            <td>'+
'                    <select class="clss" disabled name="up" id="">'+
'                        <option <{$vo["up"]==0?"selected":null}>value="0">顶级</option>'+
'                        <volist name="opts" id="opt">'+
'                            <option <{$vo["up"]==$opt["menu_id"]?"selected":null}> value="<{$opt["menu_id"]}>"><{$opt.mname}></option>'+
'                        </volist>'+
'                    </select>'+
'                </td>'+
'            <td><input name="mgroup" class="clss" disabled style="width:4em;" type="text" value=""></td>'+
'            <td><input name="msort" class="clss" disabled style="width:4em;" type="text" value=""></td>'+
'                '+
'            <td>'+
'            '+
'             <div class="switch tiny">'+
'              <input onclick="lock(this)" value="" id="lock_'+id+'" type="checkbox"  <{$vo["lock"]?"checked":""}>>'+
'                <label for="lock_'+id+'"></label>'+
'                </div>'+
'               '+
'            </td> '+
'            <td>'+
'            <button type="button" onclick="edit(\''+id+'\')" class="edit">编辑</button>'+
'            <button style="display:none" onclick="save(\''+id+'\')" type="button" class="save">保存</button>'+
'            </td>'+
'        </tr>';
 $('tbody').append(html);
            
            });

        }
        </script>
</block>